/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.components;

import DAO.PlanificacionDAO;
import Model.PuntoMapa;
import View.ventas.Frm_RegistrarPedido;
import java.awt.* ;
import java.awt.event.ActionEvent;
import java.util.List ;
import javax.swing.* ;

/**
 *
 * @author walter
 */
public class PopUpPuntoMapa extends javax.swing.JDialog {
    private JDesktopPane pnl ;
    private List<PuntoMapa> lstPuntos ;
    public PopUpPuntoMapa() {
        initComponents();
    }
    
    public PopUpPuntoMapa(JFrame parent) {
        super( parent , true ) ;
        if( parent != null ){
            Dimension parentSize = parent.getSize(); 
            Point p = parent.getLocation(); 
            setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
        }
        cargarPuntos() ;
        initComponents();
        this.pack();
        this.setVisible(true);    
    }
    
    private void cargarPuntos(){
        lstPuntos = PlanificacionDAO.getPuntosMapa() ;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        pnl = new JDesktopPane() ;
        pnlPrincipal = new javax.swing.JPanel();
        pnlMapa = new javax.swing.JPanel();

        pnlMapa.setBorder(javax.swing.BorderFactory.createTitledBorder("Mapa"));

        org.jdesktop.layout.GroupLayout pnlMapaLayout = new org.jdesktop.layout.GroupLayout(pnlMapa);
        pnlMapa.setLayout(pnlMapaLayout);
        pnlMapaLayout.setHorizontalGroup(
            pnlMapaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 934, Short.MAX_VALUE)
        );
        pnlMapaLayout.setVerticalGroup(
            pnlMapaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 377, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout pnlPrincipalLayout = new org.jdesktop.layout.GroupLayout(pnlPrincipal);
        pnlPrincipal.setLayout(pnlPrincipalLayout);
        pnlPrincipalLayout.setHorizontalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlMapa, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlPrincipalLayout.setVerticalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipalLayout.createSequentialGroup()
                .add(pnlMapa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout( pnl ) ;
        pnl.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        
        JButton puntos[] = new JButton[ lstPuntos.size() ] ;
        for(int i = 0 ; i < lstPuntos.size() ; i++){
            PuntoMapa p = lstPuntos.get( i ) ;
            puntos[ i ] = new JButton() ;
            puntos[ i ].setLocation( p.getLongitud() * 48 + 15 , p.getLatitud() * 40 + 30 ) ;
            puntos[ i ].setName( "punto" + i  ) ;
            puntos[ i ].setVisible( true ) ;
            puntos[ i ].setSize( 10 , 10 ) ;
            puntos[ i ].addActionListener( new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    puntosIActionPerformed( e ) ;
                }
            });
            pnlMapa.add( puntos[ i ] ) ;
        }
        
        this.add( pnl ) ;
    }// </editor-fold>                        
    
    public void puntosIActionPerformed( java.awt.event.ActionEvent e ){
        JButton but = (JButton)e.getSource() ;
        int idx = Integer.parseInt( but.getName().substring( 5 ) ) ;
        PuntoMapa p = lstPuntos.get( idx ) ;
        regresar( p ) ;
    }
    
    private void regresar( PuntoMapa punto ){
        Index p = ( Index )( this.getParent() ) ;
        if( ( p.getContainer().getClass().toString().equals( "class View.ventas.Frm_RegistrarPedido" ) ) ){
            try{
                this.setVisible( false ) ;
                Frm_RegistrarPedido owner = (Frm_RegistrarPedido)p.getContainer() ;
                owner.actualizarDatoPunto( punto ) ;
            }
            catch (Exception e){
            }
        }
    }
    // Variables declaration - do not modify                     
    private javax.swing.JPanel pnlMapa;
    private javax.swing.JPanel pnlPrincipal;
    // End of variables declaration                           
}
